using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A collection of flags a folder is waiting on
	/// </summary>
	public sealed class FlagCollection : DatabaseAware, IList, IEnumerable<Flag>
	{
    private string folderId;
    /// <summary>
    /// Initializes a new instance of the <see cref="FlagCollection"/> class for the specified folder.
    /// </summary>
    /// <param name="server">The server.</param>
    /// <param name="folderId">The folder ID.</param>
    public FlagCollection(Server server, string folderId) : base(server)
    {
      this.folderId = folderId;
    }

    List<Flag> flags;
    private void ReadFlags()
    {
      if (flags == null)
      {
        flags = new List<Flag>();
        using (IDataReader reader = Server.ExecuteReader(string.Format(
          "SELECT * FROM eWait WHERE eFolderID = '{0}' AND eFlagName <> ' '", folderId)))
        {
          while (reader.Read()) 
          {
            Flag flag = new Flag(reader);
            flags.Add(flag);
          }
        }
      }
    }

    /// <summary>
    /// Gets the <see cref="Flag"/> at the specified index.
    /// </summary>
    public Flag this[int index]
    {
      get
      {
        ReadFlags();
        return (Flag)flags[index];
      }
    }

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
      ReadFlags();
      return flags.GetEnumerator();
    }

    #endregion
    #region IEnumerable<Flag> Members

    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
    /// </returns>
    public IEnumerator<Flag> GetEnumerator()
    {
      ReadFlags();
      return flags.GetEnumerator();
    }

    #endregion
    #region IList Members

    /// <summary>
    /// Gets a value indicating whether the <see cref="T:System.Collections.IList"/> is read-only.
    /// </summary>
    public bool IsReadOnly
    {
      get
      {
        return true;
      }
    }

    /// <summary>
    /// Gets the <see cref="Object"/> at the specified index.
    /// </summary>
    object IList.this[int index]
    {
      get
      {
        ReadFlags();
        return flags[index];
      }
      set
      {
        throw new NotSupportedException(readOnlyListMessage);
      }
    }

    void IList.RemoveAt(int index)
    {
      throw new NotSupportedException(readOnlyListMessage);
    }

    void IList.Insert(int index, object value)
    {
      throw new NotSupportedException(readOnlyListMessage);
    }

    void IList.Remove(object value)
    {
      throw new NotSupportedException(readOnlyListMessage);
    }

    /// <summary>
    /// Determines whether the <see cref="T:System.Collections.IList"/> contains a specific value.
    /// </summary>
    /// <param name="value">The <see cref="T:System.Object"/> to locate in the <see cref="T:System.Collections.IList"/>.</param>
    /// <returns>
    /// 	<see langword="true"/> if the <see cref="T:System.Object"/>
    /// is found in the <see cref="T:System.Collections.IList"/>; otherwise, <see langword="false"/>.
    /// </returns>
    public bool Contains(object value)
    {
      ReadFlags();
      return flags.Contains((Flag)value);
    }

    void IList.Clear()
    {
      throw new NotSupportedException(readOnlyListMessage);
    }

    /// <summary>
    /// Determines the index of a
    /// specific item in the <see cref="T:System.Collections.IList"/>.
    /// </summary>
    /// <param name="value">The <see cref="T:System.Object"/> to locate in the <see cref="T:System.Collections.IList"/>.</param>
    /// <returns>
    /// The index of <paramref name="value"/> if found in the list; otherwise, -1.
    /// </returns>
    public int IndexOf(object value)
    {
      ReadFlags();
      return flags.IndexOf((Flag)value);
    }

    int IList.Add(object value)
    {
      throw new NotSupportedException(readOnlyListMessage);
    }

    /// <summary>
    /// Gets a value indicating whether the <see cref="T:System.Collections.IList"/> has a fixed size.
    /// </summary>
    public bool IsFixedSize
    {
      get
      {
        return true;
      }
    }

    #endregion
    #region ICollection Members

    /// <summary>
    /// Gets a value
    /// indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized
    /// (thread-safe).
    /// </summary>
    public bool IsSynchronized
    {
      get
      {
        return false;
      }
    }

    /// <summary>
    /// Gets the number of flags in the collection.
    /// </summary>
    public int Count
    {
      get
      {
        ReadFlags();
        return flags.Count;
      }
    }

    /// <summary>
    /// Copies the elements of
    /// the <see cref="T:System.Collections.ICollection"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
    /// </summary>
    /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
    /// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
    /// <exception cref="T:System.ArgumentNullException">
    /// 	<paramref name="array"/> is <see langword="null"/>.</exception>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    /// 	<paramref name="index"/> is less than zero.</exception>
    /// <exception cref="T:System.ArgumentException">
    /// 	<para>
    /// 		<paramref name="array"/> is multidimensional.</para>
    /// 	<para>-or-</para>
    /// 	<para>
    /// 		<paramref name="index"/> is equal to or greater than the length of <paramref name="array"/>.</para>
    /// 	<para>-or-</para>
    /// 	<para>The number of elements in the source <see cref="T:System.Collections.ICollection"/> is greater than the available space from <paramref name="index"/> to the end of the destination <paramref name="array"/>.</para>
    /// </exception>
    /// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.ICollection"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
    public void CopyTo(Flag[] array, int index)
    {
      ReadFlags();
      flags.CopyTo(array, index);
    }

    void ICollection.CopyTo(Array array, int index)
    {
      CopyTo((Flag[])array, index);
    }

    /// <summary>
    /// Gets an object that
    /// can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
    /// </summary>
    public object SyncRoot
    {
      get
      {
        return null;
      }
    }

    #endregion
  }
}
